package com.ruitu.mall_98_2.mine;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.MyRefreshBaseActivity;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.bumptech.glide.Glide;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.dao.APIRetrofit;
import com.ruitu.router_module.A_Rutils;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.PageModel;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.product.Evaluate;
import com.ruitu.router_module.bean.product.Product;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MyCommentActivity extends MyRefreshBaseActivity {
    private DataAdapter adapter;
    private List<Integer> skipId = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setLoadMoreEnabled(false);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_my_comment;
    }

    @Override
    protected void initViews() {
        super.initViews();
        setHeadTitle("我的评论");
        skipId.add(R.id.header);
        showEmptyView(skipId);
        showProgress();
        reReq();
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return findViewById(R.id.progress);
    }

    @Override
    protected void reqData() {

    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new DataAdapter(this);
        return adapter;
    }

    @Override
    protected LRecyclerView findRecyclerView() {
        return findViewById(R.id.lRecyclerView);
    }

    @Override
    protected void reqFirst() {
        reReq();
    }

    @Override
    protected void reqMore() {

    }

    @Override
    protected void reReq() {
        getData();
    }

    class DataAdapter extends ListBaseAdapter<Evaluate> {


        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_comment_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            LinearLayout ll_item = holder.getView(R.id.ll_item);
            ImageView iv_comment_goods_img = holder.getView(R.id.iv_comment_goods_img);
            TextView tv_goods_name = holder.getView(R.id.tv_goods_name);
            final Evaluate evaluate = mDataList.get(position);
            Glide.with(mContext).load(Config.BASE_URL + evaluate.getGoodsImg())
                    .apply(Arad.opts).into(iv_comment_goods_img);
            tv_goods_name.setText(evaluate.getGoodsName());
            TextView tv_comment_content = holder.getView(R.id.tv_comment_content);
            tv_comment_content.setText("内容:"+evaluate.getContent());
            TextView tv_goods_comment = holder.getView(R.id.tv_goods_comment);
            tv_goods_comment.setText(evaluate.getGoodsComment() + "分");
            TextView tv_company_comment = holder.getView(R.id.tv_company_comment);
            tv_company_comment.setText(evaluate.getCompanyComment() + "分");
            TextView tv_logistic_comment = holder.getView(R.id.tv_logistic_comment);
            tv_logistic_comment.setText(evaluate.getLogisticsComment() + "分");
            ll_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Product product = new Product();
                    product.setGoodId(evaluate.getGoodsId());
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("product", product);
                    try {
                        A_Rutils.startActivity(mContext, "com.ruitu.mall_98_1.product.activity.ProductDetailActivity", bundle);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }

    private void getData() {
        APIRetrofit.getDefault().getMineGoodsComment(AppHolder.instance().getUser().getAccountId(), "1", "100")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SubscriberImpl<BaseResult<PageModel<Evaluate>>>() {
                    @Override
                    public void onError(Throwable e) {
                        refreshComplete();
                        hideProgress();
                        ToastUtils.showShort("获取评论列表失败");
                    }

                    @Override
                    public void onNext(BaseResult<PageModel<Evaluate>> pageModelBaseResult) {
                        refreshComplete();
                        hideProgress();
                        if (pageModelBaseResult.isSuccess()) {
                            if (isDataEmpty(pageModelBaseResult.getData().getDataList())) {
                                showEmptyView(skipId);
                            } else {
                                showContent(null);
                                adapter.setDataList(pageModelBaseResult.getData().getDataList());
                            }
                        } else {
                            ToastUtils.showShort(pageModelBaseResult.getMsg());
                        }
                    }
                });
    }


}
